<?php

namespace app\home\controller;

use app\common\controller\Home;

class Teacher extends Home
{
    public $NoLogin = ['index', 'info'];

    public function __construct()
    {
        parent::__construct();

        $this->SubjectModel = model('Subject.Subject');
        $this->TeacherModel = model('Teacher.Teacher');
        $this->CategoryModel = model('Subject.Category');
        $this->FollowModel = model('Teacher.Follow');
    }
    
    //老师搜索
    public function index()
    {
        if($this->request->isAjax())
        {
            $page = $this->request->param('page', 1, 'trim');
            $limit = $this->request->param('limit', 10, 'trim');
            $keywords = $this->request->param('keywords', '', 'trim');

            //偏移量
            $start = ($page-1)*$limit;

            $where = [];

            if(!empty($keywords))
            {
                $where['name|job'] = ['LIKE', "%$keywords%"];
            }

            //查询数据总条数
            $count = $this->TeacherModel->where($where)->count();

            //查询数据
            $list = $this->TeacherModel
                ->where($where)
                ->limit($start, $limit)
                ->select();

            $data = [
                'count' => $count,
                'list' => $list
            ];

            if($list)
            {
                //成功返回
                $this->success('返回列表', null, $data);
                exit;
            }else
            {
                //失败返回
                $this->error('暂无数据', null, $data);
                exit;
            }
        }

        return $this->view->fetch();
    }

    //老师详情界面
    public function info()
    {
        $teacherid = $this->request->param('teacherid', 0, 'trim');
        
        //查询老师的信息
        $teacher = $this->TeacherModel->find($teacherid);

        if(!$teacher)
        {
            $this->error('老师信息不存在');
            exit;
        }

        //获取用户是否登录的信息
        $login = $this->IsLogin(false);

        if($login)
        {
            $where = [
                'teacherid' => $teacherid,
                'busid' => $login['id']
            ];

            //查询是否有关注过这个老师
            $check = $this->FollowModel->where($where)->find();

            //增加自定义属性
            $teacher['follow'] = $check ? true : false;
        }else
        {
            $teacher['follow'] = false;
        }

        //判断是否有ajax请求
        if($this->request->isAjax())
        {
            $page = $this->request->param('page', 1, 'trim');
            $limit = $this->request->param('limit', 10, 'trim');
            $keywords = $this->request->param('keywords', '', 'trim');

            //偏移量
            $start = ($page-1)*$limit;

            $where = ['teacherid' => $teacherid];

            if(!empty($keywords))
            {
                $where['title'] = ['LIKE', "%$keywords%"];
            }

            //查询数据总条数
            $count = $this->SubjectModel->where($where)->count();

            //查询数据
            $list = $this->SubjectModel
                ->with(['category'])
                ->where($where)
                ->limit($start, $limit)
                ->select();

            $data = [
                'count' => $count,
                'list' => $list
            ];

            if($list)
            {
                //成功返回
                $this->success('返回列表', null, $data);
                exit;
            }else
            {
                //失败返回
                $this->error('暂无数据', null, $data);
                exit;
            }
        }

        $this->assign([
            'teacher' => $teacher
        ]);

        return $this->view->fetch();
    }

    //老师关注和取消关注
    public function follow()
    {
        $teacherid = $this->request->param('teacherid', 0, 'trim');
        
        //查询老师的信息
        $teacher = $this->TeacherModel->find($teacherid);

        if(!$teacher)
        {
            $this->error('老师信息不存在');
            exit;
        }

        //判断这个人是否关注过
        $where = [
            'teacherid' => $teacherid,
            'busid' => $this->view->AutoLogin['id']
        ];

        $check = $this->FollowModel->where($where)->find();
        $result = $msg = '';

        //如果能找出记录就说明有关注过，取消关注 删除语句
        if($check)
        {

            $result = $this->FollowModel->destroy($check['id']);
            
            $msg = '取消关注成功';
        }else
        {
            //没找到记录，没有关注过，关注 插入语句
            //组装数据
            $data = [
                'teacherid' => $teacherid,
                'busid' => $this->view->AutoLogin['id']
            ];

            $result = $this->FollowModel->validate('common/Teacher/Follow')->save($data);

            $msg = '关注成功';
        }

        if($result === FALSE)
        {
            $this->error($this->FollowModel->getError());
            exit;
        }else
        {
            $this->success($msg);
            exit;
        }
    

    }
}
